  package com.gpoint.rdp.door.zoneCTL.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.db.model.DefaultQueryFilter;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.common.DoorConfig;
import com.gpoint.rdp.door.zoneCTL.service.DoorControllerService;



import com.gpoint.rdp.door.zoneCTL.service.DoorControllerQueryService;
import com.gpoint.rdp.door.CurrentPermission.persistence.entity.CurrentPermissionPo;
import com.gpoint.rdp.door.CurrentPermission.service.CurrentPermissionQueryService;
import com.gpoint.rdp.door.door.persistence.entity.DoorPo;
import com.gpoint.rdp.door.door.service.DoorQueryService;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorControllerPo;
import com.gpoint.rdp.base.core.util.AppUtil;
import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// TODO: Auto-generated Javadoc
/**
 * 服务商代码 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-26 18:33:32
 *</pre>
 */
@Controller
@RequestMapping("/rdp/zoneCTL/doorController/")
public class DoorControllerController extends GenericController{
	
	/** The door controller service. */
	@Resource
	private DoorControllerService doorControllerService;
	
	/** The door controller query service. */
	@Resource
	private DoorControllerQueryService doorControllerQueryService;	
	
	/** The door query service. */
	@Resource
	private DoorQueryService doorQueryService;
	
	/** The current permission query service. */
	@Resource
	private CurrentPermissionQueryService currentPermissionQueryService;
	
	/** The config. */
	DoorConfig config=AppUtil.getBean(DoorConfig.class);
    
    /** The servicecode. */
    String servicecode=config.getServicecode();
    
    
    /** The address. */
    @Value("${watch.address}")
    private  String address;

	/**
	 * Querysno.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("queryctl")
	public @ResponseBody PageJson querysno(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		try {
			
			//获取前台数值
			QueryFilter queryFilter = new DefaultQueryFilter();
			 
			String ctl = RequestUtil.getString(request, "ctl");
			
			 String sql =   "CONTROLLERNO ='"+ctl+"'";
			 queryFilter.addParamsFilter("whereSql", sql);
//			queryFilter.addParamsFilter("cardnum", sno);
			
			 
			//调用接口查询
			String listData = doorControllerQueryService.query(queryFilter);
			
			
			//返回结果集合
			PageList<DoorControllerPo> doorControllerList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<DoorControllerPo> list = DoorControllerPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				doorControllerList = new PageList<DoorControllerPo>(list, pageResult);
			}
			
			return new PageJson(doorControllerList);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	
	/**
	 * 【服务商代码】列表(分页条件查询)数据.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = doorControllerQueryService.query(queryFilter);
			PageList<DoorControllerPo> doorControllerList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<DoorControllerPo> list = DoorControllerPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				doorControllerList = new PageList<DoorControllerPo>(list, pageResult);
			}
			
			return new PageJson(doorControllerList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * 编辑【服务商代码】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String controllerid=RequestUtil.getString(request, "controllerid");
		DoorControllerPo doorController=null;
		if(StringUtil.isNotEmpty(controllerid)){
			String data = null;
			data = doorControllerQueryService.get(controllerid);
			doorController = DoorControllerPo.fromJsonString(data);
			//获取门列表
			List<DoorPo> doors = doorQueryService.queryByControllerSn(doorController.getControllersn());
			doorController.setDoors(doors);
		}else {
			doorController = new DoorControllerPo();
			doorController.setServicecode(servicecode);			
		}

		return getAutoView().addObject("doorController", doorController).addObject("returnUrl", preUrl).addObject("address", address);
	}
	
	
	/**
	 * 编辑【服务商代码】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("netDetail")
	public ModelAndView NetDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{	
		   String controllerid=RequestUtil.getString(request, "controllerid");
		   DoorControllerPo doorController=null;
			if(StringUtil.isNotEmpty(controllerid)){
				String data = null;
				data = doorControllerQueryService.get(controllerid);
				doorController = DoorControllerPo.fromJsonString(data);				
			}else {
				doorController = new DoorControllerPo();
				doorController.setServicecode(servicecode);			
			}		
		   return getAutoView().addObject("doorController", doorController).addObject("address",address);
	}
	
	
	/**
	 * Net detail 1.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("netDetail1")
	public ModelAndView netDetail1(HttpServletRequest request,HttpServletResponse response) throws Exception{	
		   String sn=RequestUtil.getString(request, "sn");
		   String door=RequestUtil.getString(request, "door");
		   ModelAndView mv= new ModelAndView("/rdp/door/readdoorParam.jsp") ;	
		   
		   mv.addObject("sn",sn);
		   mv.addObject("door",door);
		   mv.addObject("address",address);
		   return mv;
	}
	
	
	/**
	 * Gets the premission from device by sn and cardnum.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the premission
	 * @throws Exception the exception
	 */
	@RequestMapping("getPremission")
	public ModelAndView getPremission(HttpServletRequest request,HttpServletResponse response) throws Exception{	
		   String sn=RequestUtil.getString(request, "sn");
		   String card=RequestUtil.getString(request, "card");
		   ModelAndView mv= new ModelAndView("/rdp/door/getPremission.jsp") ;	
		   
		   mv.addObject("address",address);
		   mv.addObject("sn",sn);
		   mv.addObject("card",card);
		   return mv;
	}
	
	/**
	 * 编辑【服务商代码】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("add")
	public ModelAndView edit2(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String controllerid=RequestUtil.getString(request, "controllerid");
		DoorControllerPo doorController=null;
		if(StringUtil.isNotEmpty(controllerid)){
			String data = null;
			data = doorControllerQueryService.get(controllerid);
			doorController = DoorControllerPo.fromJsonString(data);
		}else {
			doorController = new DoorControllerPo();
			doorController.setServicecode(servicecode);			
		}

		return getAutoView().addObject("doorController", doorController).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【服务商代码】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String controllerid=RequestUtil.getString(request, "controllerid");
		DoorControllerPo doorController=null;
		if(StringUtil.isNotEmpty(controllerid)){
			String data = null;
			data = doorControllerQueryService.get(controllerid);
			doorController = DoorControllerPo.fromJsonString(data);
		}
		return getAutoView().addObject("doorController", doorController).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【服务商代码】明细页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String controllerid=RequestUtil.getString(request, "controllerid");
		DoorControllerPo doorController=null;
		if(StringUtil.isNotEmpty(controllerid)){
			String data = null;
			data = doorControllerQueryService.get(controllerid);
			doorController = DoorControllerPo.fromJsonString(data);
		}
		return getAutoView().addObject("doorController", doorController).addObject("returnUrl", preUrl);
	}
	
	/**
	 *  
	 * 保存【服务商代码】信息.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message = null;
		try {
			String json = RequestUtil.getString(request, "json");
			DoorControllerPo param = DoorControllerPo.fromJsonString(json);
			String id = param.getControllerid();
			//SN唯一校验
			DoorControllerPo orginal = doorControllerQueryService.getBySn(param.getControllersn());
			if(BeanUtils.isEmpty(orginal)||(BeanUtils.isNotEmpty(orginal)&&orginal.getControllerid().equals(id))) {
				doorControllerService.save(json);
				message=new ResultMessage(ResultMessage.SUCCESS, "保存成功");
			}else {
				message=new ResultMessage(ResultMessage.FAIL, "设备已存在");
			}
			
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "操作失败,"+e.getMessage());
			logger.error("操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【服务商代码】记录.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "controllerid");
			List<DoorControllerPo> pos = doorControllerQueryService.findByIds(Arrays.asList(ids));
			//查询当前权限表，包含控制器SN则禁止删除			
			List<String> sns = new ArrayList<String>();
			for(DoorControllerPo po:pos) {
				sns.add(po.getControllersn());				
			}					
			List<CurrentPermissionPo> permissions = currentPermissionQueryService.findByControllerSns(sns);
			if(BeanUtils.isEmpty(permissions)) {
				doorControllerService.deleteByIds(ids);
				message=new ResultMessage(ResultMessage.SUCCESS, "删除成功");
			}else {
				message=new ResultMessage(ResultMessage.FAIL, "控制器使用中");
			}

		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除失败，" + e.getMessage());
			logger.error("删除服务商代码失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	 
 	/**
 	 * List.
 	 *
 	 * @param request the request
 	 * @param response the response
 	 * @return the model and view
 	 * @throws Exception the exception
 	 */
 	@RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  return mv;
	  
	 }

	 /**
 	 * Tree.
 	 *
 	 * @param request the request
 	 * @param response the response
 	 * @return the model and view
 	 * @throws Exception the exception
 	 */
 	@RequestMapping("list2")
	 public ModelAndView tree(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv= new ModelAndView("/rdp/SearchController/searchController/list.jsp") ;	  
	  return mv;
	  
	 }
	 
	 

	 

	 
}